package com.pic.ctrl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.pic.entity.ObjResults;
import com.pic.entity.SampleInfo;
import com.pic.service.PicService;
@Controller
@RequestMapping(value = "/pic")
public class PicCtrl {

	@Autowired
	@Qualifier("picService")
	private PicService picService;

	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String savePic() {
		picService.savePic();
		return "hello";
	}

	/**
	 * 上传图片
	 * 
	 * @return
	 */
	@RequestMapping(value = "/uploadFiles")
	@ResponseBody
	public  ObjResults uploadFiles(HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		List<MultipartFile> files = multipartRequest.getFiles("files");
		return picService.saveFiles(files);
	}

	/**
	 * 获取图片
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getPics")
	@ResponseBody
	public Map<String, Object> getPics(
			@RequestParam(value = "userId", required = true) String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SampleInfo> list = new ArrayList<SampleInfo>();
		SampleInfo samp = new SampleInfo();
		samp.setImage("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_redBlue.png");
		samp.setName("bd");
		list.add(samp);
		SampleInfo samp1 = new SampleInfo();
		samp1.setImage("http://sqimg.qq.com/qq_product_operations/im/qqlogo/imlogo.png");
		samp1.setName("qq");
		samp1.setDate(new Timestamp(System.currentTimeMillis()));
		list.add(samp1);
		map.put("msg", "获取成功！");
		map.put("result", "success");
		map.put("data", list);
		return map;
	}

}
